// 添加到直播活动广播组里面
import { SUCCESS } from '../yali-sdk/utils/success';
import initYaLiWebSocket from '../yali-sdk/utils/initYaLiWebSocket';
import apiJoinLiveGroup from '../yali-sdk/business/broadcast/api/apiJoinLiveGroup';
import { FAIL } from '../yali-sdk/utils/fail';
import apiLeaveLiveGroup from '../yali-sdk/business/broadcast/api/apiLeaveLiveGroup';
import { getYaliUserInfo } from './checkAuth';
let _actionDispatcher;
// 初始化配置
export function init(actionDispatcher) {
    _actionDispatcher = actionDispatcher;
}
export function joinLiveGroup() {
    return initYaLiWebSocket().then(clientId => new Promise((rs, rj) => {
        getYaliUserInfo().then((user) => {
            if (clientId) {
                _actionDispatcher && _actionDispatcher.execute(apiJoinLiveGroup, { clientId, anchorId: user.id }, (f, fs) => {
                    // console.log('333', f, fs);
                    if (f === SUCCESS) {
                        rs && rs();
                    } else if (f === FAIL) {
                        rj && rj();
                    }
                });
            } else {
                rj && rj();
            }
        });
    }));
}

export function leaveLiveGroup() {
    return initYaLiWebSocket().then(clientId => new Promise((rs, rj) => {
        getYaliUserInfo().then((user) => {
            if (clientId) {
                _actionDispatcher && _actionDispatcher.execute(apiLeaveLiveGroup, { clientId, anchorId: user.id }, (f, fs) => {
                    if (f === SUCCESS) {
                        rs && rs();
                    } else if (f === FAIL) {
                        rj && rj();
                    }
                });
            } else {
                rj && rj();
            }
        });
    }));
}
